package laschools.team1.Zelda;

import java.awt.Image;
import java.awt.geom.Point2D;

import javax.swing.ImageIcon;

public class Link extends Character {
	/**
	 * A double which should be between 0 and 1 which represents how much of a
	 * square link will move in a step. For instance, a value of 0.5 will cause
	 * link to move half a square. 1 divided by this number should be an
	 * integer.
	 */
	private final double HOW_MUCH_MOVED = .25;

	protected static Image LinkRight1;
	protected static Image LinkRight2;
	protected static Image LinkRightAtt1;
	protected static Image LinkLeft1;
	protected static Image LinkLeft2;
	protected static Image LinkLeftAtt1;
	protected static Image LinkUp1;
	protected static Image LinkUp2;
	protected static Image LinkUpAtt1;
	protected static Image LinkDown1;
	protected static Image LinkDown2;
	protected static Image LinkDownAtt1;
	protected static Image hud;
	protected static Image fullHeartLeft;
	protected static Image fullHeartRight;
	protected static Image emptyHeartRight;
	protected static Image emptyHeartLeft;

	protected static Image currentImage;

	protected int foot;
	protected char[][] screen;
	protected int xPos, yPos;
	protected int maxHealth;

	static Point2D.Double position;

	private static boolean hasSword;

	public Link(double x, double y) {
		assignImages();
		position = new Point2D.Double(x, y);

		currentImage = LinkDown1;
		foot = 0;

		maxHealth = 6;
	}

	/**
	 * Should be called to change which imageset we're using. For instance, when
	 * Link gets the sword
	 */
	private void assignImages() {
		if (!Link.hasSword) {
			LinkRight1 = new ImageIcon(this.getClass().getResource(
					"../../../Sprites/LinkRight1.png")).getImage();
			LinkRight2 = new ImageIcon(this.getClass().getResource(
					"../../../Sprites/LinkRight2.png")).getImage();
			LinkRightAtt1 = new ImageIcon(this.getClass().getResource(
					"../../../Sprites/LinkRightAtt1.png")).getImage();
			LinkLeft1 = new ImageIcon(this.getClass().getResource(
					"../../../Sprites/LinkLeft1.png")).getImage();
			LinkLeft2 = new ImageIcon(this.getClass().getResource(
					"../../../Sprites/LinkLeft2.png")).getImage();
			LinkLeftAtt1 = new ImageIcon(this.getClass().getResource(
					"../../../Sprites/LinkLeftAtt1.png")).getImage();
			LinkUp1 = new ImageIcon(this.getClass().getResource(
					"../../../Sprites/LinkUp1.png")).getImage();
			LinkUp2 = new ImageIcon(this.getClass().getResource(
					"../../../Sprites/LinkUp2.png")).getImage();
			LinkUpAtt1 = new ImageIcon(this.getClass().getResource(
					"../../../Sprites/LinkUpAtt1.png")).getImage();
			LinkDown1 = new ImageIcon(this.getClass().getResource(
					"../../../Sprites/LinkDown1.png")).getImage();
			LinkDown2 = new ImageIcon(this.getClass().getResource(
					"../../../Sprites/LinkDown2.png")).getImage();
			LinkDownAtt1 = new ImageIcon(this.getClass().getResource(
					"../../../Sprites/LinkDownAtt1.png")).getImage();
		}
	}

	public Link() {
	}

	public void gainHealth(int amount) {
		health += amount;
		while (amount < maxHealth) {
			health--;
		}
	}

	public int getMaxHealth() {
		return maxHealth;
	}

	public void setMaxHealth(int newMax) {
		maxHealth = newMax;
	}

	/**
	 * What Link does when the up arrow is detected
	 */
	protected void keyUp() {
		screen = FrameMain.getScreen();

		// Is it time to change screens?
		if (position.y <= 0.1) {
			FrameMain.changeScreen(1);
			position.y = 10;
		}

		// Are we allowed to walk there?
		if (screen[(int) (position.x + 0.2)][(int) (position.y + 0.25)] == 'd') {
			if (screen[(int) (position.x + .75)][(int) (position.y + 0.25)] == 'd') {
				position.y -= HOW_MUCH_MOVED;
			}
		}

		// Okay, what image should I use?
		if (foot == 0) {
			currentImage = LinkUp1;
			foot = 1;
		} else if (foot == 1) {
			currentImage = LinkUp2;
			foot = 0;
		}
	}

	/**
	 * What Link does when the down arrow is detected
	 */
	protected void keyDown() {
		screen = FrameMain.getScreen();

		// Is it time to change screens?
		if (position.y >= 10) {
			FrameMain.changeScreen(3);
			position.y = 0;
		}

		// Are we allowed to walk there?
//what does the "+ 0.75" do here? I removed it with no apparent effects
		if (screen[(int) position.x][(int) (position.y + 0.75 + HOW_MUCH_MOVED)] == 'd') {
			if (screen[(int) (position.x + .75)][(int) (position.y + 0.75 + HOW_MUCH_MOVED)] == 'd') {
				position.y += HOW_MUCH_MOVED;
			}
		}

		// Okay, what image should I use?
		if (foot == 0) {
			currentImage = LinkDown1;
			foot = 1;
		} else if (foot == 1) {
			currentImage = LinkDown2;
			foot = 0;
		}
	}

	/**
	 * What Link does when the left arrow is detected
	 */
	protected void keyLeft() {
		screen = FrameMain.getScreen();

		// Is it time to change screens?
		if (position.x <= .1) {
			FrameMain.changeScreen(4);
			position.x = 15 + HOW_MUCH_MOVED;
		}

		// Are we allowed to walk there? The 0.5 in position.y allows link to
		// hang into impassable squares
//added a second case to check for the bottom of his feet, this way
//he can't partially walk to the left on non-dirt
		if (screen[(int) (position.x - HOW_MUCH_MOVED)][(int) (position.y + 0.5)] == 'd') {
			if (screen[(int) (position.x - HOW_MUCH_MOVED)][(int) (position.y + 0.99)] == 'd') {
				position.x -= HOW_MUCH_MOVED;
			}
		}

		// Okay, what image should I use?
		if (foot == 0) {
			currentImage = LinkLeft1;
			foot = 1;
		} else if (foot == 1) {
			currentImage = LinkLeft2;
			foot = 0;
		}
	}

	/**
	 * What Link does when the right arrow is detected
	 */
	protected void keyRight() {
		screen = FrameMain.getScreen();

		// Is it time to change screens?
		if (position.x >= 14.9) {
			FrameMain.changeScreen(2);
			position.x = 0 - HOW_MUCH_MOVED;
		}

		// Are we allowed to walk there?
//added a second case to check for the bottom of his feet, this way
//he can't partially walk to the right on non-dirt
		if (screen[(int) (position.x + .75 + HOW_MUCH_MOVED)][(int) (position.y + 0.5)] == 'd') {
			if (screen[(int) (position.x + .75 + HOW_MUCH_MOVED)][(int) (position.y + 0.99)] == 'd') {
				position.x += HOW_MUCH_MOVED;
			}
		}

		// Okay, what image should I use?
		if (foot == 0) {
			currentImage = LinkRight1;
			foot = 1;
		} else if (foot == 1) {
			currentImage = LinkRight2;
			foot = 0;
		}
	}

	protected static Image getLinkImage() {
		return currentImage;
	}

	protected static Point2D.Double getPosition() {
		return position;
	}
}
